package com.audaque.springboot.foshanupload.web.miniodemo.controller;

import com.audaque.springboot.foshanupload.minio.component.MinioProvider;
import com.audaque.springboot.foshanupload.minio.model.dto.MinioFileInfoDto;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zgb
 * @desc ...
 * @date 2022-03-27 22:38:53
 */
@Slf4j
@RestController
@RequestMapping("minio")
public class MinioController {
    @Autowired
    private MinioProvider minIoProvider;


    @Operation(summary = "文件上传")
    @PostMapping("/upload")
    public MinioFileInfoDto upload(@Parameter(description = "桶名（第一个文件夹）", required = true) @RequestParam("bucketName") String bucketName, @Parameter(description = "文件", required = true) @RequestBody MultipartFile file) throws Exception {
        MinioFileInfoDto minioFileInfoDto = minIoProvider.upload(file, bucketName);
        return minioFileInfoDto;
    }

    @Operation(summary = "文件签名")
    @GetMapping("/presigned")
    public String presigned(@Parameter(description = "桶名（第一个文件夹）", required = true) @RequestParam("bucketName") String bucketName,
                            @Parameter(description = "方法名", required = true) @RequestParam("methodName") String methodName,
                            @Parameter(description = "文件名", required = true) @RequestParam("fileName") String fileName) throws Exception {
        String presignedObjectUrl = minIoProvider.getPresignedObjectUrl(methodName, bucketName, fileName);
        return presignedObjectUrl;
    }

    @Operation(summary = "文件下载")
    @PostMapping("/download")
    public void download(@Parameter(description = "文件url", required = true) @RequestParam("fileUrl") String fileUrl, HttpServletResponse res) throws Exception {
        minIoProvider.download(fileUrl, res);
    }


    @Operation(summary = "文件详情")
    @GetMapping("/detail")
    public MinioFileInfoDto detail(@Parameter(description = "文件url", required = true) @RequestParam("fileUrl") String fileUrl) throws Exception {
        MinioFileInfoDto minioFileInfoDto = minIoProvider.detail(fileUrl);
        return minioFileInfoDto;
    }

    @Operation(summary = "文件删除")
    @PostMapping("/remove")
    public void remove(@Parameter(description = "文件url集合", required = true) @RequestBody List<String> fileUrlList) throws Exception {
        minIoProvider.removeObjects(fileUrlList);
    }


}
